#!/bin/bash
[ $UID == 0 ] || run=sudo
if ! [ -z $1 ] ;then
	case $1 in
		--help)
		echo "Usage:"
		echo "	`basename $0` [ directory ]"
		echo "	Will force to install lrcshow into directory."
		exit 0
		;;
		*)
		lrcdir="$1"
		[ -d $1 ] ||"$run" mkdir -m 664 -p $lrcdir
		;;
	esac
else
	for dir in {/usr,/usr/locale,/opt}/lib/totem/plugins
	do
		if [ -d $dir ];then
			lrcdir=$dir
			break
		fi
	done
fi

while [ -z "$lrcdir" ] ||! [ -d $lrcdir ]
do
	echo "\"${lrcdir}\" is not a right directory."
	echo -n "Please enter a NEW path(must be exist):"
	read lrcdir
done

while [ 1 ]
do
	echo -n "LrcShow will install to $lrcdir [yes/no]?:"
	read ch
	case $ch in
		[Yy][Ee][Ss]|[Yy])
		 break ;;
		[Nn][Oo]|[Nn])
		 echo "LrcShow was NOT installed !" 
		 exit 0 ;;
		*) ;;
	esac
done
if [ -w $lrcdir ];then
	let cr=$LINENO+7 ;
	LC_ALL=C tail -n +$cr $0 |$run tar jxf -  -C $lrcdir && echo "LrcShow was installed !" ;
else
	echo "You can have no permissions to write $lrcdir "
	exit 1
fi
exit 0
